package com.fang.networkdisk.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fang.common.common.CommonPage;
import com.fang.common.exception.ExceptionCode;
import com.fang.common.exception.NetworkDiskException;
import com.fang.common.util.CompactSnowflakeIdGenerator;
import com.fang.networkdisk.entity.bo.UserBo;
import com.fang.networkdisk.entity.po.TbNetDisUser;
import com.fang.networkdisk.entity.vo.UserVo;
import com.fang.networkdisk.mapper.TbNetDisUserMapper;
import com.fang.networkdisk.service.TbNetDisUserService;
import com.fang.networkdisk.util.NDBeanUtils;
import com.fang.networkdisk.util.PasswordUtil;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 用户表 服务实现类
 * </p>
 *
 * @author Bernie_fang
 * @since 2024-12-07
 */
@Service
public class TbNetDisUserServiceImpl extends ServiceImpl<TbNetDisUserMapper, TbNetDisUser> implements TbNetDisUserService {

    @Resource
    private TbNetDisUserMapper userMapper;

    @Override
    public TbNetDisUser findUserByUsername(String username) {
        QueryWrapper<TbNetDisUser> wrapper = new QueryWrapper<>();
        wrapper.eq("username", username).eq("deleted", false);
        return userMapper.selectOne(wrapper);
    }

    @Override
    public CommonPage queryUser(UserBo userBo) {
        userBo.setOffset((userBo.getCurPage() - 1) * userBo.getPageSize());
        List<UserVo> requestPathVos = this.userMapper.queryUser(userBo);
        int total = this.userMapper.queryUserCount(userBo);
        CommonPage result = new CommonPage(userBo.getCurPage(), total, requestPathVos);
        return result;
    }

    @Override
    public int addUser(UserBo userBo) {
        TbNetDisUser db_user = findUserByUsername(userBo.getUsername());
        if (NDBeanUtils.isObjectNotEmpty(db_user)) {
            throw new NetworkDiskException(ExceptionCode.DATA_DUPLICATE);
        }
        TbNetDisUser user = new TbNetDisUser();
        NDBeanUtils.copyListProperties(userBo, user);
        user.setUserId(CompactSnowflakeIdGenerator.getNextIdStr());
        String encryptPassword = PasswordUtil.encryption(user.getPassword());
        user.setPassword(encryptPassword);
        return userMapper.insert(user);
    }

    /**
     * Description: 修改用户账号信息，不修改用户id，用户名，密码
     * @Author: Bernie_fang
     * @Since: 2025/3/1 22:17
     * @param userBo:
     * @return: int
     **/
    @Override
    public int updateUserById(UserBo userBo) {
        TbNetDisUser user = new TbNetDisUser();
        NDBeanUtils.copyListProperties(userBo, user);
        user.setUserId(null);
        user.setUsername(null);
        user.setPassword(null);
        return userMapper.updateById(user);
    }

    @Override
    public int deleteUserById(Integer id, String authorId) {
        UpdateWrapper<TbNetDisUser> wrapper = new UpdateWrapper<>();
        wrapper.set("update_user", authorId).set("updated_time", new Date()).set("deleted", true).eq("id", id);
        return userMapper.update(null, wrapper);
    }

}
